﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dao;
using Service.Interface;

namespace Service
{
    public abstract class Service<T> : IService<T> where T : class
    {
        public IRepository<T> CurrentRepository { get; set; }

        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>实体</returns>
        public virtual T Get(object id)
        {
            if (id == null)
            {
                return null;
            }
            return this.CurrentRepository.Get(id);
        }

        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>实体</returns>
        public virtual T Load(object id)
        {
            if (id == null)
            {
                return null;
            }
            return this.CurrentRepository.Load(id);
        }

        /// <summary>
        /// 插入实体
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>ID</returns>
        public virtual object Save(T entity)
        {
            if (entity == null)
            {
                return null;
            }
            return this.CurrentRepository.Save(entity);
        }

        /// <summary>
        /// 修改实体
        /// </summary>
        /// <param name="entity">实体</param>
        public virtual void Update(T entity)
        {
            if (entity == null)
            {
                return;
            }

            this.CurrentRepository.Update(entity);
        }

        /// <summary>
        /// 修改或保存实体
        /// </summary>
        /// <param name="entity">实体</param>
        public virtual void SaveOrUpdate(T entity)
        {
            if (entity == null)
            {
                return;
            }

            this.CurrentRepository.SaveOrUpdate(entity);
        }

        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="id">ID</param>
        public virtual void Delete(object id)
        {
            if (id == null)
            {
                return;
            }

            this.CurrentRepository.Delete(id);
        }

        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="entity">实体</param>
        public virtual void Delete(T entity)
        {
            if (entity == null)
            {
                return;
            }
            this.CurrentRepository.Delete(entity);
        }

        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="IDList">ID集合</param>
        public virtual void Delete(IList<object> idList)
        {
            if (idList == null || idList.Count == 0)
            {
                return;
            }

            this.CurrentRepository.Delete(idList);
        }
    }
}
